package GUI;
 
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import logic.Server;
import logic.myFile;


public class DeletePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private Server server;
	private JLabel lblpath;
	private JLabel lblRepdeg;
	private JLabel lblnChunks;
	private JList<String> list;
	private JLabel lblfilename;

	public DeletePanel(final Server server) {
		this.setServer(server);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 121, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		JLabel lblRestoreFile = new JLabel("Delete File");
		lblRestoreFile.setFont(new Font("Tahoma", Font.PLAIN, 18));
		GridBagConstraints gbc_lblRestoreFile = new GridBagConstraints();
		gbc_lblRestoreFile.insets = new Insets(0, 0, 5, 5);
		gbc_lblRestoreFile.gridx = 3;
		gbc_lblRestoreFile.gridy = 1;
		add(lblRestoreFile, gbc_lblRestoreFile);

		list = new JList<String>(Server.getFileRepository().getFileNames());
		list.setValueIsAdjusting(true);
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				int index = list.getSelectedIndex();
				if(index!= -1){
					myFile file = Server.getFileRepository().getFiles().elementAt(index);
					if(file != null)
					{
						lblfilename.setText(file.extractNameFile());
						lblpath.setText(file.getName());
						lblRepdeg.setText(String.valueOf(file.getReplicationDeg()));
						lblnChunks.setText(String.valueOf(file.getNumChunks()));

					}
				}
				else{
					lblfilename.setText("");
					lblpath.setText("");
					lblRepdeg.setText("");
					lblnChunks.setText("");
				}
			}
		});
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setVisibleRowCount(8);
		GridBagConstraints gbc_list = new GridBagConstraints();
		gbc_list.insets = new Insets(0, 0, 5, 5);
		gbc_list.fill = GridBagConstraints.BOTH;
		gbc_list.gridx = 3;
		gbc_list.gridy = 3;
		JScrollPane optionPane = new JScrollPane(list);
		add(optionPane, gbc_list);

		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 6;
		gbc_panel.gridy = 3;
		JScrollPane infPane = new JScrollPane(panel);
		add(infPane, gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);

		JLabel lblFileName = new JLabel("File Name:");
		lblFileName.setFont(new Font("Tahoma", Font.BOLD, 11));
		GridBagConstraints gbc_lblFileName = new GridBagConstraints();
		gbc_lblFileName.anchor = GridBagConstraints.WEST;
		gbc_lblFileName.insets = new Insets(0, 0, 5, 0);
		gbc_lblFileName.gridx = 0;
		gbc_lblFileName.gridy = 0;
		panel.add(lblFileName, gbc_lblFileName);

		lblfilename = new JLabel("");
		GridBagConstraints gbc_lblfilename = new GridBagConstraints();
		gbc_lblfilename.anchor = GridBagConstraints.WEST;
		gbc_lblfilename.insets = new Insets(0, 0, 5, 0);
		gbc_lblfilename.gridx = 0;
		gbc_lblfilename.gridy = 1;
		panel.add(lblfilename, gbc_lblfilename);

		JLabel lblPath = new JLabel("Path:");
		lblPath.setFont(new Font("Tahoma", Font.BOLD, 11));
		GridBagConstraints gbc_lblPath = new GridBagConstraints();
		gbc_lblPath.anchor = GridBagConstraints.WEST;
		gbc_lblPath.insets = new Insets(0, 0, 5, 0);
		gbc_lblPath.gridx = 0;
		gbc_lblPath.gridy = 2;
		panel.add(lblPath, gbc_lblPath);

		lblpath = new JLabel("");
		GridBagConstraints gbc_lblpath = new GridBagConstraints();
		gbc_lblpath.anchor = GridBagConstraints.WEST;
		gbc_lblpath.insets = new Insets(0, 0, 5, 0);
		gbc_lblpath.gridx = 0;
		gbc_lblpath.gridy = 3;
		panel.add(lblpath, gbc_lblpath);

		JLabel lblReplicationDegreeDesired = new JLabel("Desired Replication Degree");
		lblReplicationDegreeDesired.setFont(new Font("Tahoma", Font.BOLD, 11));
		GridBagConstraints gbc_lblReplicationDegreeDesired = new GridBagConstraints();
		gbc_lblReplicationDegreeDesired.anchor = GridBagConstraints.WEST;
		gbc_lblReplicationDegreeDesired.insets = new Insets(0, 0, 5, 0);
		gbc_lblReplicationDegreeDesired.gridx = 0;
		gbc_lblReplicationDegreeDesired.gridy = 4;
		panel.add(lblReplicationDegreeDesired, gbc_lblReplicationDegreeDesired);

		lblRepdeg = new JLabel("");
		GridBagConstraints gbc_lblRepdeg = new GridBagConstraints();
		gbc_lblRepdeg.anchor = GridBagConstraints.WEST;
		gbc_lblRepdeg.insets = new Insets(0, 0, 5, 0);
		gbc_lblRepdeg.gridx = 0;
		gbc_lblRepdeg.gridy = 5;
		panel.add(lblRepdeg, gbc_lblRepdeg);

		JLabel lblNumberChunks = new JLabel("Number of Chunks");
		lblNumberChunks.setFont(new Font("Tahoma", Font.BOLD, 11));
		GridBagConstraints gbc_lblRealReplicationDegree = new GridBagConstraints();
		gbc_lblRealReplicationDegree.anchor = GridBagConstraints.WEST;
		gbc_lblRealReplicationDegree.insets = new Insets(0, 0, 5, 0);
		gbc_lblRealReplicationDegree.gridx = 0;
		gbc_lblRealReplicationDegree.gridy = 6;
		panel.add(lblNumberChunks, gbc_lblRealReplicationDegree);

		lblnChunks = new JLabel("");
		GridBagConstraints gbc_lblnChunks = new GridBagConstraints();
		gbc_lblnChunks.anchor = GridBagConstraints.WEST;
		gbc_lblnChunks.gridx = 0;
		gbc_lblnChunks.gridy = 7;
		panel.add(lblnChunks, gbc_lblnChunks);

		JButton btnRestoreFile = new JButton("Delete File");
		btnRestoreFile.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				int index = list.getSelectedIndex();
				if(index!=-1){
					myFile file = Server.getFileRepository().getFiles().elementAt(index);
					if(file!=null){
						try {
							server.deleteFile(file);
						} catch (IOException | InterruptedException e) {
							JOptionPane.showMessageDialog(GUI.frame,"Error in Delete File", "Error",JOptionPane.ERROR_MESSAGE);
						} 
						list.setListData(Server.getFileRepository().getFileNames());
					}
				}
			}
		});
		GridBagConstraints gbc_btnRestoreFile = new GridBagConstraints();
		gbc_btnRestoreFile.insets = new Insets(0, 0, 5, 5);
		gbc_btnRestoreFile.gridx = 3;
		gbc_btnRestoreFile.gridy = 4;
		add(btnRestoreFile, gbc_btnRestoreFile);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				GUI.frame.setContentPane(new MainPanel(server));

				((JPanel)GUI.frame.getContentPane()).revalidate();
				GUI.frame.repaint();
			}
		});
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.insets = new Insets(0, 0, 5, 5);
		gbc_btnCancel.gridx = 6;
		gbc_btnCancel.gridy = 4;
		add(btnCancel, gbc_btnCancel);





	}

	/**
	 * @return the server
	 */
	public Server getServer() {
		return server;
	}

	/**
	 * @param server the server to set
	 */
	public void setServer(Server server) {
		this.server = server;
	}


}
